<?php

class PhpView {
    protected
        $theme = 'default',
        $layoutPath = null,
        $var = array();
    public $pageTitle = '';
    public $pageKey = '';
    public $pageDs = '';
    public $webroot = '';
    public $layout = 'layout';
    
    /**
     * 设置布局路径
     *
     * @param string $layoutPath
     */
    public function setLayoutPath($layoutPath) {
        $this->layoutPath = $layoutPath;
    }
    
    public function set($var, $value) {
        $this->var[$var] = $value;
    }
    
    /**
     * @param    string $template    
     * @return   void
     */
    public function render($template, $admin=false) {
        foreach ($this->var as $key => $var) {
            $$key = $var;
        }
        
        ob_start();
        include($template);
        
        $content_for_layout = ob_get_clean();
        if ($this->layout) {
            //$layout = ROOT.'/theme/'.$this->theme.'/'.$this->layout.'.php';
            $this->renderLayout($content_for_layout, $admin);
        } else {
            echo $content_for_layout;
        }
    }
    
    protected function renderLayout($content_for_layout, $admin) {
        !empty($this->pageTitle) ? $pageTitle = $this->pageTitle : $pageTitle = '';
        !empty($this->pageKey) ? $pageKey = $this->pageKey : $pageKey = '';
        !empty($this->pageDs) ? $pageDs = $this->pageDs : $pageDs = '';
        if (empty($this->layoutPath)) {
            $layout = ROOT.'/theme/'.$this->theme.'/'.$this->layout.'.php';
        } else {
            $layout = $this->layoutPath.'/'.$this->layout.'.php';
        }
        include($layout);
    }
    
    protected function _render($template) {
        ob_start();
    }
}

